<?php

namespace App\Http\Controllers\Login;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Service\User\UserService;
use App\Http\Controllers\Controller;


use Session,Config;
class LoginController extends Controller
{
    public function login()
    {
        return view('Login.login');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Laravel\Lumen\Http\Redirector|void
     * 登录
     */
    public function doLogin(Request $request)
    {
        $where = [];
        $where = $request->except('_token');

        $user = new UserService();
        $data = $user->login($where);


        if($data['error_code'] == 0)
        {
            $data = $data['data'];
            Session::put('MEMBER_TOKEN',$data['token']);
            Session::put('MEMBER_INFO',$data['info']);

            return redirect('/home');

        }else{
            echo "<script>confirm('账号或密码不对，请从新登陆？')?location.href='/login' : history.back() ;</script>";
            exit();
        }
    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Laravel\Lumen\Http\Redirector|void
     *
     * 退出登录
     */
    public function logout()
    {
        $where = [];
        $where['token'] = Session::get('MEMBER_TOKEN');
        $user = new UserService();
        $data = $user->logout($where);

        if($data['error_code'] == 0)
        {
            Session::forget('MEMBER_TOKEN');
            Session::forget('MEMBER_INFO');
            return redirect('/');
        }else{
            echo "<script>confirm('退出失败!')?location.href='/' : history.back() ;</script>";
            exit();
        }
    }



}
